package gstyle.compressor;

import gstyle.Style;
import gstyle.compressor.CssCompressor;

import java.util.Map;

public class BorderCompressor implements CssCompressor {
    public Map compress(Map attributes) {
        String border = (String) attributes.remove(Style.BORDER);
        String top = (String) attributes.remove(Style.BORDER_TOP);
        String right = (String) attributes.remove(Style.BORDER_RIGHT);
        String bottom = (String) attributes.remove(Style.BORDER_BOTTOM);
        String left = (String) attributes.remove(Style.BORDER_LEFT);
        String width = (String) attributes.remove(Style.BORDER_WIDTH);
        String style = (String) attributes.remove(Style.BORDER_STYLE);
        String color = (String) attributes.remove(Style.BORDER_COLOR);
        String compressedBackground = border(border, width, style, color, top, right, bottom, left);
        if (!"".equals(compressedBackground)) {
            attributes.put("border", compressedBackground);
        }
        return attributes;
    }

    private String border(String border, String width, String style, String color, String top, String right, String bottom, String left) {
        String fullBorder = blankIfNull(border)
                + blankIfNull(width)
                + blankIfNull(style)
                + blankIfNull(color)
                + blankIfNull(top)
                + blankIfNull(right)
                + blankIfNull(bottom)
                + blankIfNull(left);

        return fullBorder.equals("") ? "" : fullBorder.trim();
    }

    private String blankIfNull(String attribute) {
        return attribute == null ? "" : attribute + " ";
    }

}